//
//  ILSaveTool.m
//  ItheimaLottery
//
//  Created by wang on 15/5/18.
//  Copyright (c) 2015年 Bitenergy. All rights reserved.
//

#import "BGSaveTool.h"
#import "NSString+Dir.h"

@implementation BGSaveTool

+ (void)setObject:(id)value forKey:(NSString *)defaultName{
    [[NSUserDefaults standardUserDefaults] setObject:value forKey:defaultName];
    [[NSUserDefaults standardUserDefaults] synchronize];

}

+ (id)objectForKey:(NSString *)defaultName
{
    return [[NSUserDefaults standardUserDefaults] objectForKey:defaultName];
}


+ (void)setArchiverObject:(id)value forKey:(NSString *)defaultPathName
{
    NSString *docPath =  [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *modelPath = [docPath stringByAppendingPathComponent:defaultPathName];

    // 0.3.将自定义对象保存到文件中
    [NSKeyedArchiver archiveRootObject:value toFile:modelPath];
}

+ (id)objectArchiverForKey:(NSString *)defaultPathName
{
    //路径
    NSString *docPath =  [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *modelPath = [docPath stringByAppendingPathComponent:defaultPathName];
    
    return [NSKeyedUnarchiver unarchiveObjectWithFile:modelPath];
}

+ (void)removeArchiverObjectForKey :(NSString *)defaultPathName{
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *userPath = [docPath stringByAppendingPathComponent:defaultPathName];
    NSFileManager *manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath:userPath]) {
        [manager removeItemAtPath:userPath error:nil];
    }
}

+ (void)setCacheArchiverObject:(id)value forKey:(NSString *)defaultPathName
{
    // 0.3.将自定义对象保存到文件中
    [NSKeyedArchiver archiveRootObject:value toFile:[defaultPathName appendCacheDir]];
}

+ (id)objectCacheArchiverForKey:(NSString *)defaultPathName
{
     return [NSKeyedUnarchiver unarchiveObjectWithFile:[defaultPathName appendCacheDir]];
}

@end
